package com.example.demo.elasticsearch;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName BlogServiceImpl
 * @Description:
 * @Author liyunjiang
 * @Date 2020/5/17 21:16
 */
@Service
public class BlogServiceImpl implements BlogService{
    @Resource
    BlogRepository blogRepository;
    @Override
    public void add(BlogEntity entity) {
        blogRepository.save(entity);
    }

    @Override
    public BlogEntity getById(String id) {
        BlogEntity blogEntity = blogRepository.findById(id).orElse(null);
        if (blogEntity != null){
            return blogEntity;
        }
        return null;
    }

    @Override
    public List<BlogEntity> list() {
        Iterable<BlogEntity> all = blogRepository.findAll();
        List<BlogEntity> list = new ArrayList<>();
        all.forEach(list::add);
        return list;
    }

    @Override
    public void update(BlogEntity entity) {
        blogRepository.save(entity);
    }

    @Override
    public List<BlogEntity> findByTitle(String keywords) {

        return blogRepository.findByTitleLike(keywords);
    }
}
